<template>
	<view class="container">
		<!-- 标题 -->
		<view class="title">
			<h3>智引未来-大学生就业成长平台</h3>
		</view>

		<!-- 平台图标容器 -->
		<view class="logo-container">
			<image :src="imgsrc" class="platform-logo" mode="aspectFit" />
		</view>

		<!-- 表单区域 -->
		<u--form class="form-container" :model="model1" :rules="rules" ref="uForm" :errorType="toast">

			<!-- 学号输入 -->
			<u-form-item class="input-wrapper" prop="user.studentId">
				<u-input v-model="model1.user.studentId" placeholder="请输入学号" class="rounded-input"
					placeholder-style="font-size: 28rpx;" shape="circle">
					<template #prefix>
						<u-icon name="account" size="34" color="#7f8388" margin-right="10"></u-icon>
					</template>
				</u-input>
			</u-form-item>



			<!-- 密码输入框 -->
			<u-form-item class="input-wrapper" prop="user.password">
				<u-input v-model="model1.user.password" placeholder="请输入密码" password class="rounded-input"
					placeholder-style="font-size: 28rpx;" shape="circle">
					<template #prefix>
						<u-icon name="lock" size="34" color="#7f8388" margin-right="10"></u-icon>
					</template>
				</u-input>
			</u-form-item>

			<!-- 登录按钮 -->
			<u-button type="primary" shape="circle" @click="handleLogin" class="login-btn">登录</u-button>

			<!-- 微信登录按钮 -->
			<u-button type="success" shape="circle" icon="weixin-fill" icon-color="#ffffff" @click="handleWechatLogin"
				class="wechat-btn">微信一键式登录</u-button>

			<!-- 底部链接 -->
			<view class="bottom-links">
				<text class="link">没有账号 <text class="register-link" @click="goToRegister">去注册</text></text>
				<text class="link" @click="goToForget">忘记密码？</text>
			</view>
		</u--form>
	</view>
</template>

<script>
	import {
		studentIDLogin
	} from '../../../api/loginModel/loginModel';
	import {mapState,mapMutations} from 'vuex'
	export default {
		data() {
			return {
				imgsrc:this.$baseURL+"/profile/upload/2025/05/10/logo_20250507082032A011_20250510150253A033.jpg",
				model1: {
					user: {
						studentId: '', // 学号
						password: '', // 密码
						code: ''
					}
				},
				loginData: {
					studentID: "",
					password: "",
					code: ""
				},
				rules: {
					'user.studentId': {
						type: 'string',
						required: true,
						message: '请填写学号',
						//change：字段值发生变化时校验   blur：输入框失去焦点时触发
						trigger: ['blur', 'change']
					},
					'user.password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						//change：字段值发生变化时校验   blur：输入框失去焦点时触发
						trigger: ['blur', 'change']
					},
				},
			}
		},
		methods: {
			...mapMutations(["holdIswxlogin"]),
			
			// 处理按学号登录
			handleLogin() {
				this.$refs.uForm.validate().then(res => {

					setTimeout(() => {
						console.log(this.model1.user);
						uni.hideLoading();
						this.loginData.studentID = this.model1.user.studentId
						// this.loginData.code = this.model1.user.code
						this.loginData.password = this.model1.user.password
						// 发送登录请求
						studentIDLogin(this.loginData).then((res) => {
							if (res.code == 200) {
								// 标记我的页面显示修改密码
								this.holdIswxlogin(0)
								// 登录成功设置token
								uni.setStorageSync("token", res.data.token)
								uni.showToast({
									title: '登录成功',
									icon: 'success'
								});
								uni.reLaunch({
									url: '/pages/index/index'
								});

							} else {
								// 登录失败
								// 登录失败时调用
								// uni.showToast({
								// 	title: '学号或密码错误',
								// 	icon: 'none', // 使用 'none' 避免默认的对勾图标
								// 	duration: 2000 // 显示时间 2 秒
								// });
								// console.log(res.msg)
							}
						})
					}, 1500);

				}).catch(errors => {
					uni.$u.toast(errors[0].message);
				});
			},




			// 处理微信登录
			handleWechatLogin() {
				uni.showLoading({
					title: '微信登录中...'
				});

				setTimeout(() => {
					uni.hideLoading();

					//        已注册，绑定学号的
					// map.put("abcdefg","123456");
					// map.put("sssdsdd","123244");
					//        已注册，未绑定学号的
					// map.put("ssdwew","4588554");
					//        未注册的
					// map.put("a123456","a123456");//微信绑定的手机号已存于数据库中，绑定失败
					// map.put("b123456","b123456");
					// map.put("c123456","c123456");
					// map.put("d123456","d123456");

					this.model1.user.code = "abcdefg"
					studentIDLogin(this.model1.user).then((res) => {
						if (res.code == 200) {
							// 登录成功
							uni.showToast({
								title: '微信登录成功',
								icon: 'success'
							});
							// 标记我的页面不显示修改密码
							this.holdIswxlogin(1)
							// 设置token
							uni.setStorageSync("token", res.data.token)
							// 判断是否跳转到wxbind页面
							if (res.data.haveStudentID === "1") {
								// 需要绑定学号
								uni.reLaunch({
									url: '/pages/loginModel/wxBind/wxBind'
								});

							} else {
								// 不需要绑定学号
								uni.reLaunch({
									url: '/pages/index/index'
								});
							}

						} else {
							// 登录失败
							// uni.showToast({
							// 	title: res.msg,
							// 	icon: 'none',
							// 	duration: 2000
							// });
						}

					})


				}, 500);

			},


			// 跳转到注册页面
			goToRegister() {
				uni.navigateTo({
					url: '/pages/loginModel/register/register'
				});
			},

			// 跳转到忘记密码页面
			goToForget() {
				uni.navigateTo({
					url: '/pages/loginModel/restPassword/restPassword'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 40rpx;
		min-height: 100vh;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.title {
		font-size: 40rpx;
		font-weight: bold;
		text-align: center;
		margin: 40rpx 0;
		color: #333;
	}

	.logo-container {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 20rpx 0 60rpx;

		.platform-logo {
			width: 200rpx;
			height: 200rpx;
			border-radius: 24rpx;
		}
	}

	.form-container {
		width: 100%;
		padding: 0 30rpx;
	}

	.u-form-item__body[data-v-5e7216f1] {
		width: 20px;
	}

	.input-wrapper {
		position: relative;


		.rounded-input {
			background-color: #fff;
			border-radius: 50rpx;
			padding: 0 20rpx;
			height: 90rpx;
			border: 1px solid #e6e6e6;
			box-shadow: 0 4rpx 12rpx 0 rgba(0, 0, 0, 0.3);

			::v-deep .u-input__content__field-wrapper {
				height: 100%;
				display: flex;
				align-items: center;
			}

			::v-deep .u-input__content__field-wrapper__field {
				height: 100%;
				flex: 1;
				padding-left: 10rpx;
			}

			::v-deep .u-input__content__field-wrapper__clear-icon {
				margin-right: 10rpx;
			}

			::v-deep .u-input__content__field-wrapper__prefix-icon {
				margin-right: 10rpx;
			}
		}
	}

	/deep/ .u-button--circle.data-v-3bf2dba7 {
		margin-bottom: 10px;
	}

	.login-btn {
		font-size: 30rpx;
		margin-top: 40rpx;
		height: 100rpx;
		border-radius: 50rpx;
		background-color: #2979ff;
		width: 100%;
		color: #fff;
		border: none;

		box-shadow: 0 4rpx 12rpx 0 rgba(0, 0, 0, 0.3);
	}

	.wechat-btn {
		font-size: 30rpx;
		margin-bottom: 40rpx;
		margin-top: 30rpx;
		height: 100rpx;
		border-radius: 50rpx;
		background-color: #00ff00;
		width: 100%;
		color: #fff;
		border: none;
		box-shadow: 0 4rpx 12rpx 0 rgba(0, 0, 0, 0.3);


	}

	.bottom-links {
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;

		.link {
			color: #2979ff;
			font-size: 26rpx;
		}

		.register-link {
			color: #ff0000;
			font-size: 26rpx;
		}
	}
</style>